package com.laikan.reader.core.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.facebook.drawee.view.SimpleDraweeView;
import com.laikan.reader.R;
import com.laikan.reader.core.LaikanBaseAdapter;
import com.laikan.reader.core.bean.RankingBean;
import com.laikan.reader.core.utils.PublicUtil;

import java.util.ArrayList;

/**
 * 排行榜适配器
 * Created by gwt on 2016/10/19.
 */
public class RankingAdapter extends LaikanBaseAdapter<RankingBean.DataBean>{

    private ViewHolder holder ;
    private ArrayList<Integer> rankIcons = new ArrayList<>(); //本地图片集合
    private Bitmap layout_right;
    private Drawable drawable_layout_right_img;
    private ImageView right_arrow;

    public RankingAdapter(Context context, ArrayList<RankingBean.DataBean> datas) {
        super(context,datas);
        rankIcons.add(R.drawable.hotcakes_boy);
        rankIcons.add(R.drawable.hotcakes_girg);
        rankIcons.add(R.drawable.hotcakes_chuban);
        rankIcons.add(R.drawable.hotcakes_update);
        rankIcons.add(R.drawable.hotcakes_count);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.item_ranking_list, null);
            right_arrow = (ImageView)convertView.findViewById(R.id.right_arrow);
            layout_right = PublicUtil.readBitMap(mContext, R.drawable.lk_enter_small);
            drawable_layout_right_img = new BitmapDrawable(PublicUtil.getAlphaBitmap(layout_right, Color.parseColor("#cccccc")));
            right_arrow.setImageDrawable(drawable_layout_right_img);
            holder.iv_icon = (SimpleDraweeView)convertView.findViewById(R.id.iv_icon);
            holder.rank_one = (TextView)convertView.findViewById(R.id.rank_one);
            holder.rank_two = (TextView)convertView.findViewById(R.id.rank_two);
            holder.rank_there = (TextView)convertView.findViewById(R.id.rank_there);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder)convertView.getTag();
        }
        RankingBean.DataBean dataBean = datas.get(position);
        String imgUrl = dataBean.getImgUrl();

        if(TextUtils.isEmpty(imgUrl)){
            //网络没有图片 , 显示本地的
            if(dataBean.getSeq() == 1){
                holder.iv_icon.setImageResource(rankIcons.get(0));
            }
            if(dataBean.getSeq() == 2){
                holder.iv_icon.setImageResource(rankIcons.get(1));
            }
            if(dataBean.getSeq() == 3){
                holder.iv_icon.setImageResource(rankIcons.get(2));
            }
            if(dataBean.getSeq() == 4){
                holder.iv_icon.setImageResource(rankIcons.get(3));
            }
            if(dataBean.getSeq() == 5){
                holder.iv_icon.setImageResource(rankIcons.get(4));
            }
        }else{
            //显示网络的
            holder.iv_icon.setImageURI(Uri.parse(imgUrl));
        }

        if(dataBean.getRankList()!=null && dataBean.getRankList().size()!=0){
            holder.rank_one.setText(dataBean.getRankList().get(0).getName());
            holder.rank_two.setText(dataBean.getRankList().get(1).getName());
            holder.rank_there.setText(dataBean.getRankList().get(2).getName());
        }

        return convertView;
    }

    private class ViewHolder {
        private TextView rank_one ,rank_two ,rank_there;
        private SimpleDraweeView iv_icon;
    }

}
